package com.jhaosdk;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.cocos2dx.lib.Cocos2dxHelper;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class WechatSdkManager {
    private static final String TAG = "WechatSdkManager";

    public  static  WechatSdkManager instance = new WechatSdkManager();

    // APP_ID 替换为你的应用从官方网站申请到的合法appID
    public static final String APP_ID = "wxa16c47ab675b6edb";


    public IWXAPI api;

    private static final int THUMB_SIZE = 150;
    public static void init(){
//        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WechatSdkManager.instance.api = WXAPIFactory.createWXAPI(Cocos2dxHelper.getActivity(), APP_ID, true);
                WechatSdkManager.instance.api.registerApp(APP_ID);
            }
        });
    }

    public static void login(final String state){
        Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // send oauth request
                Log.i("WechatSdkManager", "login=="+state);
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = state;
                WechatSdkManager.instance.api.sendReq(req);
            }
        });
    }

    /**
     * @param title 分享的title
     * @param text 分享的内容
     * @param description 分享的描述
     * @param scene 分享的地方  0:分享到对话: 1:分享到朋友圈: 2:分享到收藏:
     */
    public static  void share_url(String jsonStr){
        Log.i("WechatSdkManager", "share_url=="+jsonStr);
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            final String title = jsonObject.getString("title");
            final String url = jsonObject.getString("url");
            final String description = jsonObject.getString("description");
            final int scene = jsonObject.getInt("scene");
            Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WXWebpageObject webpageObject = new WXWebpageObject();
                    webpageObject.webpageUrl = url;
                    WXMediaMessage msg = new WXMediaMessage();
                    msg.title = title;
                    msg.description = description;
                    msg.mediaObject = webpageObject;
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.message = msg;
                    req.scene = scene;
                    WechatSdkManager.instance.api.sendReq(req);
                }
            });
        }catch (JSONException ex){
            Log.w("WechatSdkManager", "pay error no json");
        }
    }


    /**
     * @param scene 分享的地方  0:分享到对话: 1:分享到朋友圈: 2:分享到收藏:
     */
    public static void share_img(String jsonStr){
        Log.i("WechatSdkManager", "share_img=="+jsonStr);
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            final String picPath = jsonObject.getString("picPath");
            final int scene = jsonObject.getInt("scene");
            Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(picPath);
                    if (!file.exists()) {
                        Log.w("WechatSdkManager", "share_img-error图片不存在");
                        Toast.makeText(Cocos2dxHelper.getActivity(), "分享图片不存在", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Bitmap bmp = BitmapFactory.decodeFile(picPath);

                    //初始化 WXImageObject 和 WXMediaMessage 对象
                    WXImageObject imgObj = new WXImageObject(bmp);
                    WXMediaMessage msg = new WXMediaMessage();
                    msg.mediaObject = imgObj;

                    //设置缩略图
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
                    bmp.recycle();
                    msg.thumbData = ImageUtil.bmpToByteArray(thumbBmp, true);

                    //构造一个Req
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.message = msg;
                    req.scene = scene;
//                    req.
                    WechatSdkManager.instance.api.sendReq(req);
                }
            });
        }catch (JSONException ex){
            Log.w("WechatSdkManager", "pay error no json");
        }

    }

    public void handle_callback(String code, String function){
        WechatSdkManager.callbackToWechatSdkManager(function, code);
    }

    /**
     * ----------------------------------------------  回调给游戏端_微信-----------------------------------------
     */
    public static void callbackToWechatSdkManager(final String function, final String result) {
        Cocos2dxHelper.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Log.i("callbackToGame", "function="+function+" result="+ result);
                Cocos2dxJavascriptJavaBridge.evalString("WechatSdkManager." + function + "('" + result + "')");
            }
        });
    }

}
